package milkmidi.papervision3d.view
{	
	import flash.display.Loader;
	import flash.display.MovieClip;
    import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.utils.getQualifiedClassName;
	import org.papervision3d.core.proto.CameraObject3D;
	import org.papervision3d.cameras.CameraType;
	import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.Papervision3D;
	import org.papervision3d.view.BasicView
	import org.papervision3d.view.Viewport3D;
	

    public class BasicViewX extends BasicView
	{		
		public static var debug :Boolean = false;
		private var _isRender	:Boolean = true;
		private var _isActive	:Boolean = true;
		
		

        public function BasicViewX( viewWidth:Number = 0 , viewHeight:Number = 0, autoScale:Boolean = true, interactive:Boolean = false , cameraType:String = "Target"):void		{
			super(viewWidth, viewHeight, autoScale, interactive, cameraType);
 			trace(this + ".Consturctor()", " cameraType:" + cameraType + " " + camera);
			this.addEventListener(Event.ADDED_TO_STAGE, onAdd2Stage , false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage , false, 0, true);
			startRendering();		
        }
		public function destroy():void {			
			viewport.destroy();			
			if(parent){
				parent.removeChild(this);
			}
		}
		
		public function set render(p_bol:Boolean):void{
			_isRender = p_bol;
			if (_isRender) {
				startRendering()
			}else {
				stopRendering(false, false);
			}
			if(debug) trace("BasicViewX.render: " + bol);
		}
		public function get render():Boolean{
			return _isRender;
		}
		
		public function get mouseXHalf():Number {
			if (!stage) {
				return 0;
			}
			return 	stage.stageWidth/2 - stage.mouseX;
		}
		public function get mouseYHalf():Number {
			if (!stage) {
				return 0;
			}
			return stage.stageHeight/2 - stage.mouseY;;
		}	
		protected function onAdd2Stage(e:Event):void	{
			this.removeEventListener(Event.ADDED_TO_STAGE, onAdd2Stage);
			stage.addEventListener(Event.ACTIVATE, onActivate);
			stage.addEventListener(Event.DEACTIVATE, onDeActivate);
		}
		protected function onRemoveFromStage(e:Event):void	{
			this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
			stage.removeEventListener(Event.ACTIVATE, onActivate);
			stage.removeEventListener(Event.DEACTIVATE, onDeActivate);
		}
		private function onActivate(e:Event):void{
			_isActive = true;
			if(debug) trace("BasicViewX.isActive: " + _isActive);
		}
		private function onDeActivate(e:Event):void	{
			_isActive = false;
			if(debug) trace("BasicViewX.isActive: " + _isActive);
		}
	
		protected function extraRender3D():void { }				
		public function setQuality(bol:Boolean): void{
			if (stage == null){
				return;
			}
			
            if (!bol)
			stage.quality = StageQuality.LOW;
			else
			stage.quality = StageQuality.HIGH;
        }		
		override protected function onRenderTick(e:Event = null):void{
			if (!_isActive) return;
			if (!_isRender) return;
			extraRender3D();
			super.onRenderTick(e);
		}		
		public override function toString():String{
			return getQualifiedClassName(this);
		}
    }
}
